package com.simon.netty.demo2;

import com.simon.netty.demo2.Utils.ByteBufUtils;
import com.simon.netty.demo2.po.Person;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * Created by changf on 2017/7/14.
 */
public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {


    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Person person=new Person();
        person.setName("simon常");
        person.setAge(27);
        ctx.write(person);
        ctx.flush();
        ctx.close();

    }


    //服务器接受数据后
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
        System.out.println("接受server data");
        byte[] bytes = ByteBufUtils.ByteBuffToBytes(byteBuf);
        System.out.println(new String(bytes,"UTF-8"));
    }
}
